<?php
namespace Controller;

use Hoa\Registry\Registry;
use Library\Input;
use Library\Log;
use Library\View;
use Model\Dict;
use Service\DictService;
use Validator\DictValidator;


/**
 * DictController
 * 数据字典
 */
class DictController extends BaseController {
	/**
	 * 构造函数
	 */
	public function __construct() {
		parent::__construct();
	}


	/**
	 * test
	 */
	public function test() {
		echo __METHOD__, '<br />';
	}


	/**
	 * 字典列表
	 */
	public function dict() {
		View::display('Dict/dict');
	}


	/**
	 * 新增字典
	 */
	public function addDict() {
		View::display('Dict/addDict');
	}


	/**
	 * 处理新增字典
	 */
	public function doAddDict() {
		//定义变量
		$conf = Registry::get('conf');
		$ret = $conf['status'][200];

		//获取参数
		$code = I('post.code');
		$name = I('post.name');
		$type = I('post.type');
		$remark = I('post.remark');

		//验证参数
		//验证编码
		$validateCode_result = DictValidator::validateCode($code);
		if (true !== $validateCode_result) {
			exit(json_encode($validateCode_result));
		}

		//验证名称
		$validateName_result = DictValidator::validateName($name);
		if (true !== $validateName_result) {
			exit(json_encode($validateName_result));
		}

		//验证类型
		$validateType_result = DictValidator::validateType($type);
		if (true !== $validateType_result) {
			exit(json_encode($validateType_result));
		}

		//处理新增字典
		$data = array(
			'code' => $code,
			'name' => $name,
			'type' => $type,
			'remark' => $remark,
		);

		//查询是否已存在相同的编码
		$same_dict = Dict::getByCode($code);
		if ($same_dict) {
			$ret = $conf['status'][410401];
			exit(json_encode($ret));
		}
		$result = DictService::addDict($data);
		if (!$result) {
			$ret = $conf['status'][510201];	 //新增保存失败
			exit(json_encode($ret));
		}
		exit(json_encode($ret));
	}


	/**
	 * 批量新增字典
	 */
	public function batchAddDict() {
		View::display('Dict/batchAddDict');
	}


	/**
	 * 处理批量新增字典
	 */
	public function doBatchAddDict() {
		//定义变量
		$conf = Registry::get('conf');
		$ret = $conf['status'][200];
		$dict_list = [];

		//获取参数
		$code_list = I('post.code');
		$name_list = I('post.name');
		$type_list = I('post.type');
		$remark_list = I('post.remark');

		for ($i = 0; $i < count($code_list); $i++) {
			$dict = ['code' => $code_list[$i],
				'name' => $name_list[$i],
				'type' => $type_list[$i],
				'remark' => $remark_list[$i],
			];
			$dict_list[] = $dict;
		}

		//批量添加
		$result = DictService::batchAddDict($dict_list);
		if ($result !== true) {
			$ret = $conf['status'][410401];
			$ret['i'] = $result;
			exit(json_encode($ret));
		}
		exit(json_encode($ret));
	}


	/**
	 * 处理删除配置
	 */
	public function doDelDict() {
		//定义变量
		$conf = Registry::get('conf');
		$ret = $conf['status'][200];

		//获取参数
		$id = I('post.id');

		//删除配置
		DictService::delDict($id);
		exit(json_encode($ret));
	}


	/**
	 * 处理批量删除配置
	 */
	public function doBatchDelDict() {
		//定义变量
		$conf = Registry::get('conf');
		$ret = $conf['status'][200];

		//获取参数
		$id_list = I('post.id_list');

		//批量删除
		$result = DictService::batchDelDict($id_list);
		if (!$result) {
			$ret = $conf['status'][510202];
			exit(json_encode($ret));
		}
		exit(json_encode($ret));
	}


	/**
	 * 修改配置
	 */
	public function editDict() {
		//获取参数
		$id = I('get.id');

		//查询配置
		$dict = DictService::getDict($id);

		//Success
		$data = [];
		$data['dict'] = json_encode($dict);
		View::display('Dict/editDict', $data);
	}


	/**
	 * 处理修改配置
	 */
	public function doEditDict() {
		//定义变量
		$conf = Registry::get('conf');
		$ret = $conf['status'][200];

		//获取参数
		$id = I('post.id');
		$code = I('post.code');
		$name = I('post.name');
		$type = I('post.type');
		$remark = I('post.remark');

		//验证参数
		//验证编码
		$validateCode_result = DictValidator::validateCode($code);
		if (true !== $validateCode_result) {
			exit(json_encode($validateCode_result));
		}

		//验证名称
		$validateName_result = DictValidator::validateName($name);
		if (true !== $validateName_result) {
			exit(json_encode($validateName_result));
		}

		//验证类型
		$validateType_result = DictValidator::validateType($type);
		if (true !== $validateType_result) {
			exit(json_encode($validateType_result));
		}

		//修改配置
		$data = [
			'code' => $code,
			'name' => $name,
			'type' => $type,
			'remark' => $remark,
		];

		//检查是否已存在相同的编码
		$same_dict = DictService::findSameEditDict($code, $id);
		if ($same_dict) {
			$ret = $conf['status'][410401];   //编码已存在
			exit(json_encode($ret));
		}
		$result = DictService::saveDict($data, $id);
		if (!$result) {
			$ret = C('status.510201');
			exit(json_encode($ret));
		}
		exit(json_encode($ret));
	}


	/**
	 * 批量修改配置
	 */
	public function batchEditDict() {
		//获取参数
		$id_list = I('get.id_list');

		//查询配置列表
		$dict_list = DictService::getDictList($id_list);

		//Success
		$data = [];
		$data['dict_list'] = $dict_list;
		View::display('Dict/batchEditDict', $data);
	}


	/**
	 * 处理批量修改配置
	 */
	public function doBatchEditDict() {
		//定义变量
		$conf = Registry::get('conf');
		$ret = $conf['status'][200];
		$dict_list = [];

		//获取参数
		$id_list = I('post.id');
		$code_list = I('post.code');
		$name_list = I('post.name');
		$type_list = I('post.type');
		$remark_list = I('post.remark');

		for ($i = 0; $i < count($id_list); $i++) {
			$dict = ['id' => $id_list[$i],
					'code' => $code_list[$i],
					'name' => $name_list[$i],
					'type' => $type_list[$i],
					'remark' => $remark_list[$i],
			];
			$dict_list[] = $dict;
		}

		//批量添加
		$result = DictService::batchEditDict($dict_list);
		if ($result !== true) {
			$ret = $conf['status'][410401];
			$ret['id'] = $result;
			exit(json_encode($ret));
		}
		exit(json_encode($ret));
	}


	/**
	 * 搜索字典
	 */
	public function searchDict() {
		//获取参数
		$keyword = Input::post('keyword', '');
		$page = Input::post('page/d', 1);
		$pagesize = Input::post('pagesize/d', 15);
		Log::debug(__METHOD__ . ': ' . __LINE__ . ': $keyword = ' . var_export($keyword, true));

		//搜索字典
		$result = DictService::searchDict($keyword, $page, $pagesize);
		$this->successJsonReturn($result);
	}


	/**
	 * 高级搜索字典
	 */
	public function advanceSearchDict() {
		//定义变量
		$conf = Registry::get('conf');
		$env_pagesize = $conf['base']['pagesize'];

		//获取参数
		$code = Input::post('code');
		$name = Input::post('name');
		$type = Input::post('type');
		$page = Input::post('page', 1);
		$pagesize = Input::post('pagesize', $env_pagesize);

		$param = [
			'code' => $code,
			'name' => $name,
			'type' => $type,
		];

		//搜索配置
		$result = DictService::advanceSearchDict($param, $page, $pagesize);

		//Success
		$this->successJsonReturn($result);
	}



}